<?php
class ActionHelper extends AppHelper{
    var $servicespath="http://192.168.1.153:8088/axis2/services/GetFile?wsdl";
    var $flag=0;
    //上传文件
    //path 维数组类型 Array ( [name] => Array ( [0] => attach.png ) [type] => Array ( [0] => image/png ) [tmp_name] => Array ( [0] => D:\XAMPP\tmp\php180.tmp ) [error] => Array ( [0] => 0 ) [size] => Array ( [0] => 34695 ) ) 
    function upload_file($path,$dir,$index=0,$flag=0)
    {	 
    	$dir = "/var/vcap/store/fss_backend1/df327e98-f3c3-4b5c-ac53-2042211673db/".$dir;  
    	
    	if (!file_exists($dir)){
    		
    		mkdir(FIlESYSTEM_PATH."upload", 0777);
    		mkdir(FIlESYSTEM_PATH."images", 0777);		
    		mkdir($dir, 0777);
    	} 
    	
        $name="";$tmp_name="";$type="";$size="";
//		$name=$index?$path["name"][$index]:$path['name'];
//  	$tmp_name=$index?$path["tmp_name"][$index]:$path["tmp_name"];
//		$type=$index?$path["type"][$index]:$path["type"];
//		$size=$index?$path["size"][$index]:$path["size"];
		$name= is_array($path["name"])?$path["name"][$index]:$path["name"];
		$tmp_name=is_array($path["tmp_name"])?$path["tmp_name"][$index]:$path["tmp_name"];
		$type=is_array($path["type"])?$path["type"][$index]:$path["type"];
		$size=is_array($path["size"])?$path["size"][$index]:$path["size"];
	
		if($flag==1){
			$this->checkfiletype($type);
		}
		
		$index = strrpos($name,'.');       
		$retval=substr($name, $index+1, strlen($name) - $index);
		$rename=$this->uuid().".".$retval;
		$uploadfile = $dir.$rename;

		if(move_uploaded_file($tmp_name,$uploadfile)){
			return $rename;
		}else if (copy($tmp_name,$uploadfile)){
			return $rename;
		}else{
			return false;
		}
    }
    function checkfiletype($type)
    {
        if($type!="image/pjpeg" && $type!="image/jpeg" && $type!="image/gif" && $type!="image/png" && $type!="image/x-png")//文件类型不在指定范围
        {
            $this->showJsMessage("上传文件只可以是JPEG或GIF或PNG类型的!");
            exit;
        }
    }
    function uuid($prefix = '')  
    {  
        $chars = md5(uniqid(mt_rand(), true));  
        $uuid  = substr($chars,0,8) . '-';  
        $uuid .= substr($chars,8,4) . '-';  
        $uuid .= substr($chars,12,4) . '-';  
        $uuid .= substr($chars,16,4) . '-';  
        $uuid .= substr($chars,20,12);  
        return $prefix . $uuid;  
    }    
    function showJsMessage($msg)
    {
        echo "<html><head>
    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
    <title>提示</title>
    <script language=javascript>" ;
        echo 'window.alert("' . $msg . '");window.history.back();';
        echo "</script></head></html>";
    }
    function downloads($file_title,$file_path){
      //$file_path = "/var/vcap/store/fss_backend1/dc55e8f1-6c68-4cdd-a667-7c0c8a386af4/".$file_path;
       $file_path = "/var/vcap/store/fss_backend1/df327e98-f3c3-4b5c-ac53-2042211673db/".$file_path;
     // $file_path='E:/jchen/'.$file_path;     
    	if (!file_exists($file_path)){
                header("Content-type: text/html; charset=utf-8");
                //echo "File not found!";
                $this->showJsMessage("没有找到文件");
                exit;
            } else {
                header("Pragma: public");    
                Header("Accept-Ranges: bytes");
                Header("Accept-Length: ".filesize($file_path));
                $ua = $_SERVER["HTTP_USER_AGENT"]; 
                $filename = $file_title; 
                $encoded_filename = urlencode($filename); 
                $encoded_filename = str_replace("+", "%20", $encoded_filename); 
                header('Content-Type: application/octet-stream'); 
                if (preg_match("/MSIE/", $ua)) { 
                    header('Content-Disposition: attachment; filename="' . $encoded_filename . '"'); 
                } else if (preg_match("/Firefox/", $ua)) { 
                    header('Content-Disposition: attachment; filename*="utf8\'\'' . $filename . '"'); 
                } else { 
                    header('Content-Disposition: attachment; filename="' . $filename . '"'); 
                } 
                $file = fopen($file_path,"r");
                echo fread($file, filesize($file_path));
                fclose($file);
            }
    }
    
    function checktype($user,$flag){
//        switch($flag){
//            case "type":
//                if($user['usertype']!='2'&& $user['usertype']!='3'){
//                return true;
//                }
//            }
//         return false;
			return true;
        }
    
    function checkPermissions($user,$type,$userid=null)
    {
        switch($type){
            case "add":
           // var_dump($user);exit;
                if($user['isadmin'] || @$user['iscareer']||$user['isgroupadmin']){return true;}
                break;
            case "edit":
                if($user['isadmin']||$user['isgroupadmin']||$user["id"]==$userid){return true;}
                break;
            case "delete":
                if($user['isadmin'] || $user['isgroupadmin']||$user["id"]==$userid){return true;}
                break;
            case "application":
                if($user["id"]==$userid){return true;}
                break;
            case "confirm":
                if($user['isadmin'] || $user['isgroupadmin']){return true;}
                break;
            case "utype":
               // if($user['usertype']!='2'&& $user['usertype']!='3'){return true;}
               return true;
                break;
        }
        return false;
    }
        
//		if($user['isadmin'] || $user['isgroupadmin'] || $user['cid']==$id){
//			return true;
//		}
//		return true;
//	}
    function getnavigation($controller,$actions)
    {
        $menu=classregistry::init('Constant')->getMenu($controller);
        return $menu; 
    }
    
	//异步提交数据
    function ybsend($url){
    	
        $fp = fsockopen("ljrctest.sturgeon.mopaas.com", "80", $errno, $errstr, 30);
        if (!$fp) {
            echo "$errstr ($errno)<br />\n";exit();
        } else {
            $out = "GET $url / HTTP/1.1\r\n";
            $out .= "Host: ljrctest.sturgeon.mopaas.com\r\n";
            $out .= "Connection: Close\r\n\r\n";
            fwrite($fp, $out);
            /*忽略执行结果
            while (!feof($fp)) {
            echo fgets($fp, 128);
            }*/
            fclose($fp);
        }
    }
    
    function sendemial($title,$content,$action,$email){
    		$to=$email;
    		$result=$this->sendmail($title,$content,'lsdiao@163.com',$to);
    		if($result){
    			$this->flash($title."发送邮箱，请查收", "");
    		}
  }
    
    

private function sendmail($title,$content,$from,$to,$charset='utf-8',$attachment =''){
		App::import('Sendemail', 'PHPMailer.class');		
		header('Content-Type: text/html; charset='.$charset);
		$mail = new PHPMailer();
		$mail->CharSet = $charset;
		$mail->IsSMTP();
		$mail->Host = "smtp.163.com";
		$mail->Port = 25;
		$mail->From = $from;
		$mail->FromName = "";
		$mail->SMTPAuth = true;
		$mail->Username = $from;
		$mail->Password = "boy123456";
		$mail->Subject = $title;
		$mail->AltBody = "text/html";
		$mail->Body = $content;
		$mail->IsHTML(true);
		$mail->WordWrap = 50;
		$mail->AddReplyTo("lsdiao@163.com","Mopaas");
		$mail->AddAddress($to);
		if ($attachment != '') 
		{
			$mail->AddAttachment($attachment, $attachment);
		}
		if(!$mail->Send())
		{
			return false;
		} else {
			return true;
		}
		
	}
    
    
    
    
}
?>